;$(function() {
	var id = $("#id").val();
	getInfo(id);
})

function getInfo(id){
	   $.ajax({
        url: ctxPath+"/agency/dept/findById/"+id,
        type: 'GET',
        success: function(d){
        	var success = d.success;
        	 if(success==true){
        	 var data=  d.data;
        	 if(data!=null){
        		 $("#name").html(data.name);
        		 $("#address").html(data.address);
        		 $("#tel").html(data.tel);
        		 $("#sort").html(data.sort);
        		 $("#content").html(data.basicFunction);
            		 if(data.files!=null){
               			 var img = "";
               			 var url=ctxPath+"/file/showImage/";
               			 for(var i=0;i<data.files.length;i++){
               				 var temp = url+data.files[i];
               				 img+='<li>'
                 			+'<a href="'+temp+'" data-type="image">'
            				+'<img src="'+temp+'" alt="" width="100px;">'
            			    +'</a>'
            			    +'</li>'  ;
               			 }
               	    $("#dropzone").html(img);
               	 $('.plusview').plusview({
             		height : 400,
             		hide : "hide",
             		show : "show"
             	});
               	}	 
        	 }
        	}
        } 
    });
}


function submitForm(){
	$("#global-form").validator({
		rules: {
			mobile: function(element, params) {
				var tel =/^(0[0-9]{2,3}\-)([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$/;
			    return tel.test(element.value) || '请检查电话号码';
			},
		},
	    fields: {
	    	name:{
				rule : "required;",
				msg : {
					required : "必填!"
				}
			},
			address:{
				rule : "required;",
				msg : {
					required : "必填!"
				}
			},
			tel:{
				rule : "required;mobile",
				msg : {
					required : "必填!"
				}
			},
			sort:{
				rule : "required;integer",
				msg : {
					required : "必填!"
				}
			},
	    },
//	    fields: {},
	    invalid: function(form, errors){
	    	console.log(errors);
	    	$(".btn-submit").removeAttr("style");
	    },
	    //验证成功
	    valid: function(form) {
	    	 saveInfo();
	    },
	});
}

function saveInfo(){
	var json =$("#global-form").serializeFormJSON();
	console.log(json);
	var id = $("#id").val();
	var url= ctxPath+"/agency/dept/saveInfo";
	if(id!=''&&id!=null){
		url = ctxPath+"/agency/dept/updateInfo";
	}
    $.ajax({
    	url: url,
        type: 'POST',
        data: json,
        success: function(d){
        	var success = d.success;
        	if(success){
        	tale.alertOk({
                       text:'文章保存成功',
                       then: function () {
                           setTimeout(function () {
                               window.location.href = ctxPath+'/agency/dept/list';
                           }, 500);
                       }
                   });
        	}else{
        		tale.alertError(result.message || '保存失败');
        	}
        }
    });
}
